<!DOCTYPE html>  
<html>
<head>  
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>显示位置</title>  
    <style type="text/css">  
    html{height:100%}  
    body{height:100%;margin:0px;padding:0px}  
    #container{height:100%}  
    </style>
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=MdoIa8D65EPO8bq66h6Nj5N9r4hAulxL">
    //v2.0版本的引用方式：src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"
    </script>
</head>  
 
<body>  
<div id="container"></div> 
<script type="text/javascript">

// var date = '2017-11-10 20:40',
//     lon = 118.73251738,
//     lat = 31.97559901;

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

var query = GetRequest();
var phone = query['phone'];
var start = query['start'];
var stop = query['stop'];
console.log('phone: ' + phone + ', start: ' + start + ', stop: ' + stop);

function update() {
    console.log('update: ');
    // 查询最后一次gps
    $.getJSON('/gpssignin/querygps?phone='+phone+'&start='+start+'%2000:00:00&stop='+stop+'%2023:59:59', function(data) {
        console.log('data: ' + data);
        if (data.length) {
            draw(data);
        } else {
            alert('无GPS数据');
            window.history.back(-1);
        }
    });
}

function draw(locations) {
    var map = new BMap.Map("container");
    map.centerAndZoom(new BMap.Point(118.73251738, 31.97559901), 11); // TODO xuzhou 
    map.enableScrollWheelZoom(); // 启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom(); // 启用地图惯性拖拽，默认禁用

    var pointsView = []; //存放点的数组

    locations.forEach(function(location) {
        // 创建地图实例  
        var point = new BMap.Point(location.lon, location.lat);
        pointsView.push(point);
        var marker = new BMap.Marker(point);  // 创建标注
        map.addOverlay(marker);              // 将标注添加到地图中
        marker.addEventListener("click", function() {
            console.log('click marker');
            // alert(''+location.time); // TODO display time
            var opts={width:10,height:10,title:'时间'}; 
            var infoWindow = new BMap.InfoWindow(''+location.time, opts); 
            map.openInfoWindow(infoWindow,point); 
        });
    })

    map.setViewport(pointsView); //将所有的点放置在最佳视野内
}

update();

</script>  
</body>  
</html>

